<?php

namespace App\Service\User\v1_2_1;

use App\Constants\ErrorCode;
use App\Helpers\AliyunUtil;
use App\Helpers\ConfigUtil;
use App\Model\CloseFriend;
use App\Model\CoinLog;
use App\Model\ProfileVerifyLog;
use App\Model\Service\ConsumeService;
use App\Model\UserCoin;
use App\Service\BaseService;

/**
 * 查看微信
 */
class CheckWechatService extends BaseService
{
    protected function RoVerifyList()
    {
        return [
            'profile_user_id' => 'required|int'
        ];
    }

    public function handle()
    {
        $profileUserId = $this->request->input('profile_user_id', 0);
        $confirm = $this->request->input('confirm', 0);
        //亲密度
        $closeValue = CloseFriend::getCloseValue($this->userId, $profileUserId);
        $closeValueLmt = $this->checkWhite() ? 99 : 666;
        if ($closeValue < $closeValueLmt) {
            log_user_result($this->userId, "not_close");
            return $this->setError(ErrorCode::CLOSE_VALUE_LMT, "亲密值达{$closeValueLmt}°C可以申请查看她的社交账号，多增加互动，例如文字聊天，视频聊天，语音聊天和送礼物可以更快提高亲密值~", ['close_value' => (string)$closeValue]);
        }

        $result = ProfileVerifyLog::checkVerified($profileUserId, ProfileVerifyLog::TYPE_WECHAT, 'data');
        if (!$result || $result->status != ProfileVerifyLog::STATUS_OK) {
            log_user_result($this->userId, "no_wechat");
            return $this->setError(ErrorCode::WECHAT_NOT_SUBMIT, '对方微信正在认证中，审核完成即可查看哦～');
        }

        //未确认，则返回价格
        $costCoin = ConfigUtil::getWechatCheckCoin($profileUserId);

        //已经支付过该主播的微信查看费用
        $check = CoinLog::checkConsumeFrom($this->userId, $profileUserId, CoinLog::FROM_CHECK_WECHAT_OUT);
        if ($check || $this->checkWhite()) {
            $data = json_decode($result->content, true);
            $ret = [
                'pay' => 1,
                'wechat_account' => $data['account'],
                'wechat_qrcode' => AliyunUtil::IMG_HOST . $data['qrcode'],
                'cost_coin' => $costCoin
            ];
            !$confirm && $ret['cost_coin'] = $costCoin;
            log_user_result($this->userId, $ret);
            return $ret;
        } elseif (!$confirm) {
            log_user_result($this->userId, "costcoin");
            return ['cost_coin' => $costCoin];
        }

        //金币不足
        $myCoin = UserCoin::getUserInfo($this->userId);
        if ($myCoin['total_coin'] < $costCoin) {
            return $this->setError(ErrorCode::COIN_NOT_ENOUGH, '剩余金币不够查看, 可以先充值哦~');
        }

        //支付
        $payRet = ConsumeService::checkWechat($this->userId, $profileUserId, $costCoin);
        if (!$payRet) {
            return $this->setError(ErrorCode::COST_COIN_ERR, '支付金币发生错误，请稍后重试～');
        }

        $data = json_decode($result->content, true);
        log_user_result($this->userId, $data);
        return [
            'wechat_account' => $data['account'],
            'wechat_qrcode' => AliyunUtil::IMG_HOST . $data['qrcode'],
        ];
    }

    function checkWhite()
    {
        return in_array($this->userId, [73673]);
    }
}
